package com.sddnytsh.body_buildingsystem.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sddnytsh.body_buildingsystem.topic.RunData
import java.time.format.DateTimeFormatter

@Composable
fun CaloriesChart(data: List<RunData>) {
    // 确保数据不为空
    if (data.isEmpty()) {
        Text("暂无数据", modifier = Modifier.fillMaxWidth(), textAlign = TextAlign.Center)
        return
    }

    val maxCalories = data.maxOf { it.calories }.coerceAtLeast(1) // 防止除零错误
    val chartHeight = 150.dp

    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(12.dp),
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                "每日卡路里消耗",
                style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold)
            )

            Spacer(modifier = Modifier.height(20.dp))

            // 图表容器
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(220.dp)
            ) {
                // Y轴刻度
                Column(
                    modifier = Modifier
                        .width(40.dp)
                        .fillMaxHeight(),
                    verticalArrangement = Arrangement.SpaceBetween
                ) {
                    Text("$maxCalories", fontSize = 12.sp, color = Color.Gray)
                    Text("${maxCalories / 2}", fontSize = 12.sp, color = Color.Gray)
                    Text("0", fontSize = 12.sp, color = Color.Gray)
                }

                // 柱状图
                Row(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(start = 40.dp), // 为Y轴留出空间
                    horizontalArrangement = Arrangement.SpaceEvenly,
                    verticalAlignment = Alignment.Bottom
                ) {
                    data.forEach { dayData ->
                        val heightRatio = dayData.calories.toFloat() / maxCalories
                        val barHeight = (heightRatio * chartHeight.value).dp

                        Column(
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Bottom,
                            modifier = Modifier.width(30.dp)
                        ) {
                            // 显示卡路里数值在柱状图上方
                            Text(
                                text = "${dayData.calories}",
                                fontSize = 11.sp,
                                fontWeight = FontWeight.Bold,
                                modifier = Modifier.padding(bottom = 4.dp)
                            )

                            // 柱状图
                            Box(
                                modifier = Modifier
                                    .width(20.dp)
                                    .height(barHeight)
                                    .background(
                                        color = Color(0xFFF44336),
                                        shape = RoundedCornerShape(topStart = 4.dp, topEnd = 4.dp)
                                    )
                            )

                            // 日期
                            Text(
                                text = dayData.date.format(DateTimeFormatter.ofPattern("MM/dd")),
                                fontSize = 10.sp,
                                color = Color.Gray,
                                modifier = Modifier.padding(top = 6.dp),
                            )
                        }
                    }
                }

                // X轴基线
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(1.dp)
                        .background(Color.LightGray)
                        .align(Alignment.BottomStart)
                        .padding(start = 40.dp) // 与Y轴对齐
                )
            }

            Spacer(modifier = Modifier.height(8.dp))

            Text(
                "最近${data.size}天卡路里消耗趋势",
                fontSize = 12.sp,
                color = Color.Gray,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}